Bee.data.DWRProxy = function(config){
    this.call = config.call;
    this.dosave = config.save;
    this.doupdate = config.update;
    this.params = config.params;
    this.ownerCt = config.ownerCt;
    this.addEvents({
        beforesave: true,
        save: true,
        saveexception: true,
        beforeupdate: true,
        update: true,
        updateexception: true
    });
    Bee.data.DWRProxy.superclass.constructor.call(this);
};

Ext.extend(Bee.data.DWRProxy, Ext.data.DataProxy, {
    load: function(params, reader, callback, scope, arg){
        if (this.fireEvent("beforeload", this, params) !== false) {
            var delegate = this.loadResponse.createDelegate(this, [reader, callback, scope, arg], 1);
            var callParams = [];
            if (params.filter) {
                callParams.push(params.filter);
            }
            if (arg && arg.arg) {
                if (arg.arg instanceof Array) {
                    callParams = arg.arg.slice();
                }
                else {
                    callParams.push(arg.arg);
                }
            }
            else 
                if (params && params.query) {
                    var paramsObj = {
                        query: params.query
                    };
                    for (var obj in this.params) {
                        paramsObj[obj] = this.params[obj];
                    }
                    callParams.push(paramsObj);
                }
                else 
                    if (this.params) {
                        if (this.params instanceof Array) {
                            callParams = this.params.slice();
                        }
                        else {
                            callParams.push(this.params);
                        }
                    }
            if (this.ownerCt) {
                var ownerCt = this.ownerCt;
                delegate = {
                    callback: delegate
                };
            }
            callParams.push(delegate);
            this.call.apply(this, callParams);
        }
        else {
            callback.call(scope || this, null, arg, false);
        }
    },
    loadResponse: function(response, reader, callback, scope, arg){
        var result;
        try {
            result = reader.read(response);
        } 
        catch (e) {
            this.fireEvent("loadexception", this, null, response, e);
            callback.call(scope, null, arg, false);
            return;
        }
        callback.call(scope, result, arg, true);
    },
    save: function(params, callback, scope, arg){
        if (this.fireEvent("beforesave", this, params) !== false) {
            var delegate = this.saveResponse.createDelegate(this, [callback, scope, arg], 1);
            var callParams = [];
            if (params.filter) {
                callParams.push(params.filter);
            }
            if (arg && arg.arg) {
                if (arg.arg instanceof Array) {
                    callParams = arg.arg.slice();
                }
                else {
                    callParams.push(arg.arg);
                }
            }
            else 
                if (this.params) {
                    if (this.params instanceof Array) {
                        callParams = this.params.slice();
                    }
                    else {
                        callParams.push(this.params);
                    }
                }
            callParams.push(delegate);
            this.dosave.apply(this, callParams);
        }
        else {
            callback.call(scope || this, null, arg, false);
        }
    },
    
    saveResponse: function(response, callback, scope, arg){
        callback.call(scope, response, arg, true);
    },
    update: function(params, callback, scope, arg){
        if (this.fireEvent("beforeupdate", this, params) !== false) {
            var delegate = this.updateResponse.createDelegate(this, [callback, scope, arg], 1);
            var callParams = [];
            if (params.filter) {
                callParams.push(params.filter);
            }
            if (arg && arg.arg) {
                if (arg.arg instanceof Array) {
                    callParams = arg.arg.slice();
                }
                else {
                    callParams.push(arg.arg);
                }
            }
            else 
                if (this.params) {
                    if (this.params instanceof Array) {
                        callParams = this.params.slice();
                    }
                    else {
                        callParams.push(this.params);
                    }
                }
            callParams.push(delegate);
            this.doupdate.apply(this, callParams);
        }
        else {
            callback.call(scope || this, null, arg, false);
        }
    },
    updateResponse: function(response, callback, scope, arg){
        callback.call(scope, response, arg, true);
    }
});
